package com.xtaller.lazy.anync;

import com.alibaba.fastjson.JSONObject;
import com.xtaller.lazy.lib.bean.JWTResult;
import com.xtaller.lazy.lib.convert.J;
import com.xtaller.lazy.lib.kit.JwtUtil;
import com.xtaller.lazy.model.system.DataLog;
import com.xtaller.lazy.service.system.DataLogService;
import io.jsonwebtoken.Claims;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @version V1.0
 * @author: Taller
 * @date: 2019-04-22
 * @Description: 数据日志
 */
@Component
public class LogTask {
    @Autowired private DataLogService logService;

    @Async("taskExecutor")
    public void setLog(JSONObject object, JSONObject ret){
        var result = ret.getJSONObject("result");
        var message = ret.getString("message");
        var id = result.getLong("id") == null ? 0L : result.getLong("id");
        var token = object.getString("token");
        JWTResult jwt = JwtUtil.validateJWT(token);
        Claims claims = jwt.getClaims();
        var log = new DataLog();
        log.setData(J.o2s(result));
        log.setMethod(object.getString("method"));
        log.setParams(J.o2s(object.get("params")));
        log.setUrl(object.getString("url"));
        log.setIp(object.getString("ip"));
        log.setMessage(message);
        log.setDataId(id);
        log.setTime(object.getInteger("time"));
        log.setCreateId(Long.parseLong(claims.getId()));
        logService.save(log);
    }
}
